<template>
  <div>
    <el-table :data="consultationList" border style="width: 100%">
      <el-table-column type="index" label="ID" width="50"></el-table-column>
      <el-table-column prop="title" label="诊断"></el-table-column>
      <el-table-column prop="consultationType" label="咨询类型" width="180">
        <template slot-scope="scope">
          <template v-for="(value, key) in scope.row.consultationType">
            {{ key ? ' / ' : '' }}{{ value }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          {{showStatus(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="时间" width="170"></el-table-column>
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
          <el-button @click="info(scope.row.id)" size="mini">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="margin-top-20 text-center" v-if="consultationList.length">
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="currentPageChange"
        :background="true"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total">
      </el-pagination>
    </div>
    <el-dialog title="问诊详情" :visible.sync="dialogConsultationVisible" width="70%" append-to-body>
      <Info :consultation="consultationDetail" :member="member" :doctor="doctor" :messages="resetMessageFormatForShow(consultationDetail.chat_content)"/>
    </el-dialog>
  </div>
</template>

<script>
import Info from "@/components/pet/consultation/Info.vue";

export default {
  data() {
    return {
      consultationList: [],
      consultationDetail: {},
      member: {},
      doctor: {},
      dialogConsultationVisible: false,
      page: {
        total: 0,
        currentPage: 1,
        size: 10
      }
    };
  },
  props: ["total", "inputPetId"],
  components: {
    Info
  },
  mounted() {
    this.getConsultationInfoData(this.inputPetId);
  },
  methods: {
    getConsultationInfoData(inputPetId) {
      this.axios
        .get("/doctor/consultations/pet/" + inputPetId + "/list", {
          params: {
            pageSize: this.page.size,
            currentPage: this.page.currentPage
          }
        })
        .then(response => {
          if (response.data.code == 200) {
            this.consultationList = response.data.data.data;
            this.page.total = response.data.total;
            this.member = response.data.data.member;
            this.doctor = response.data.data.doctor;
          } else {
            this.$message({
              message: response.data.msg,
              type: "error"
            });
          }
        });
    },

    pageSizeChange(val) {
      this.page.currentPage = 1;
      this.page.size = val;
      this.getConsultationInfoData(this.inputPetId);
    },
    currentPageChange(val) {
      this.page.currentPage = val;
      this.getConsultationInfoData(this.inputPetId);
    },
    info(id) {
      this.getConsulationResult(id);
      this.dialogConsultationVisible = true;
    },

    getConsulationResult(consulationID) {
      this.axios
        .get("/doctor/consultations/" + consulationID)
        .then(response => {
          if (response.data.code == 200) {
            this.consultationDetail = response.data.data.consultation;
            this.member = response.data.data.member;
            this.doctor = response.data.data.doctor;
          } else {
            this.$message({
              message: response.data.msg,
              type: "error"
            });
          }
        });
    },
    resetMessageFormatForShow(messages) {
      if(messages){
        var messagesArray = JSON.parse(messages);
        var newMessage = [];
        var tempKeyObject = {};
        var tempValueObject = {};
        for (var i = 0; i < messagesArray.length; i++) {
          var shopKey = Object.keys(messagesArray[i]).indexOf("shop");
          if (shopKey >= 0) {
            tempKeyObject = {
              from: "商店",
              body: messagesArray[i].shop,
              time: messagesArray[i].time
            };
          } else {
            tempKeyObject = {
              from: "医生",
              body: messagesArray[i].doctor,
              time: messagesArray[i].time
            };
          }
          newMessage.push(tempKeyObject);
        }
        return newMessage.reverse();
      }
      else{
        return [];
      }
    },
    showStatus(row) {
      switch (row.status) {
        case 1:
          return "已提问";
          break;
        case 2:
          return "待回答";
          break;
        case 3:
          return "已回答";
          break;
        case 4:
          return "已确认";
          break;
        default:
          return "无";
          break;
      }
    }
  }
};
</script>
